﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using RenShiPinYongBiao.hrdbDataSetTableAdapters;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace RenShiPinYongBiao
{
    class DataSourcePinYong
    {
        protected DataTable dt;
        protected DataTable genderDict;
        protected DataTable nationDict;
        protected DataTable partyDict;
        protected DataTable degreeDict;
        protected DataTable eduLevelDict;
        protected Image photo = null;

        public Image Photo
        {
            get { return photo; }
            set { photo = value; }
        }
        public DataSourcePinYong(DataTable dt)
        {
            this.dt = dt;
            LoadDictTable();
        }
        public virtual List<string> PrepareText(int index)
        {
            List<string> texts = new List<string>();
            DataRow dr = dt.Rows[index];
            texts.Add("姓名");
            texts.Add(dr["FName"].ToString());
            texts.Add("性别");
            texts.Add(FindNameByCode(dr["FGenderCode"], this.genderDict));
            texts.Add("民族");
            texts.Add(FindNameByCode(dr["FNationCode"], this.nationDict));
            texts.Add("出生年月");
            texts.Add(FormatDate(dr["FBirthday"]));
            texts.Add("参加工作时间");
            texts.Add(this.FormatDate(dr["FFirstWorkDate"]));
            texts.Add("政治面貌");
            texts.Add(FindNameByCode(dr["FPartyCode"], this.partyDict));

            bool has = false;
            DataTable degreeTable = new TDegreeTableAdapter().GetDataByIdAndMax(dr["FEmployeeID"].ToString());
            if (degreeTable.Rows.Count > 0)
            {
                has = true;
            }
            texts.Add("学位");
            texts.Add(has ? FindNameByCode(degreeTable.Rows[0]["FDegreeCode"], this.degreeDict) : "");
            has = false;
            DataTable edudt = new TEduStoryTableAdapter().GetDataById(dr["FEmployeeID"].ToString());
            DataRow edudr = null;
            if (edudt.Rows.Count > 0)
            {
                has = true;
                edudr = edudt.Rows[0];
            }
            texts.Add("学历");
            texts.Add(has ? FindNameByCode(edudr["FEduLevelCode"], this.eduLevelDict) : "");
            texts.Add("毕业院校及专业");
            texts.Add(has ? edudr["FUniversity"] + "\r\n" + edudr["FSpecialty"] : "");
            texts.Add("现户籍所在地");
            texts.Add("");
            DataTable wsdt = new TWorkStoryTableAdapter().GetDataById(dr["FEmployeeID"].ToString());
            has = false;
            DataRow wsdr = null;
            if (wsdt.Rows.Count > 0)
            {
                has = true;
                wsdr = wsdt.Rows[0];
            }
            texts.Add("聘用前工作单位、职务及职称");
            texts.Add(has ? wsdr["FCompany"] + "   " + wsdr["FHeadShip"] + "   " + wsdr["FTitle"] : "");
            texts.Add("拟聘岗位及等级");
            texts.Add("初级");
            texts.Add("聘期");
            texts.Add("一年");
            texts.Add("工作简历");
            StringBuilder sb = new StringBuilder();
            if (has)
            {

                for (int i = wsdt.Rows.Count - 1; i >= 0; i--)
                {
                    DataRow r = wsdt.Rows[i];

                    sb.Append(this.FormatDate(r["FStartDate"], true));
                    sb.Append("--");
                    sb.Append(this.FormatDate(r["FEndDate"], true));
                    sb.Append("");
                    sb.Append(r["FCompany"]);
                    sb.Append("   ");
                    sb.Append(r["FHeadShip"].Equals(DBNull.Value) ? "      " : r["FHeadShip"]);
                    sb.Append("\r\n");
                }
            }
            texts.Add(has ? sb.ToString() : "");
            has = false;

            texts.Add("家庭成员及其主要社会关系");
            sb = new StringBuilder();
            DataTable hdt = new THouseHoldTableAdapter().GetDataById(dr["FEmployeeID"].ToString());
            foreach (DataRow row in hdt.Rows)
            {
                sb.Append(row["FRelation"]);
                sb.Append("   ");
                sb.Append(row["FName"]);
                sb.Append("   ");
                sb.Append(row["FCompany"]);
                sb.Append("   ");
                sb.Append(row["FHeadShip"]);
                sb.Append("\r\n");
            }
            texts.Add(sb.ToString());
            //添加照片
            if(photo!=null)
                photo.Dispose();
            photo = null;
            if (!dr["FPhoto"].Equals(System.DBNull.Value))
            {
                try
                {
                    photo = Image.FromStream(new MemoryStream((Byte[])dr["FPhoto"]));
                    Clipboard.SetImage(ImageHelp.CutForCustom(photo, 75, 112));
                }
                catch (Exception)
                {
                    Clipboard.SetText(" ");
                }
            }
            else
            {
                Clipboard.SetText(" ");
            }
            return texts;
        }
        public static string FindCodeByName(object name, DataTable t)
        {
            if (name.Equals(System.DBNull.Value))
            {
                return "";
            }
            foreach (DataRow row in t.Rows)
            {
                if (row[1].ToString().Equals(name.ToString()))
                {
                    return row[0].ToString();
                }
            }
            return "";
        }

        public static string FindNameByCode(Object code, DataTable t)
        {
            if (code.Equals(System.DBNull.Value))
            {
                return "";
            }
            foreach (DataRow row in t.Rows)
            {
                if (row[0].ToString().Equals(code.ToString()))
                {
                    return row["FName"].ToString();
                }
            }
            return "";
        }
        protected string FormatDate(Object date)
        {
            if (date.Equals(System.DBNull.Value))
            {
                return "";
            }
            return date.ToString().Substring(0, 7).Replace('-', '.');
        }
        protected string FormatDate(Object date, bool fill)
        {
            if (fill && FormatDate(date).Equals(""))
            {
                return "       ";
            }
            return FormatDate(date);
        }
        protected void LoadDictTable()
        {
            this.genderDict = new TDictGenderTableAdapter().GetData();
            this.nationDict = new TDictNationTableAdapter().GetData();
            this.partyDict = new TDictPartyTableAdapter().GetData();
            this.degreeDict = new TDictDegreeTableAdapter().GetData();
            this.eduLevelDict = new TDictEduLevelTableAdapter().GetData();
        }
    }
}
